const db = wx.cloud.database();
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
     display: "block",
        page: 38,  
   distance: 0, // 手指移动的距离
    scale: 1, // 缩放比例
    baseWidth: '', // 图片实际宽度
    baseHeight: '', // 图片实际高度
    initWidth: '', // 图片默认显示宽度
    initHeight: '', // 图片默认显示高度
    scaleWidth: '', // 图片缩放后的宽度
    scaleHeight: '', // 图片缩放后的高度
       
    },
  
/**
   * 监听图片加载成功时触发
   */
  imgload: function (e) {
    this.multiple = e.detail.width / this.width; // 计算原图和默认显示的倍数
    let height = this.multiple > 1 ? e.detail.height / this.multiple : e.detail.height; // 等比例计算出默认高度
    let width = this.multiple > 1 ? this.width : e.detail.width;
    this.setData({
      baseWidth: e.detail.width, // 获取图片实际宽度
      baseHeight: e.detail.height, // 获取图片实际高度
      initWidth: width,
      initHeight: height,
      scaleWidth: width,
      scaleHeight: height,
    })
  },
  /**
   * 双手指触发开始 计算开始触发两个手指坐标的距离
   */
  touchstartCallback: function (e) {
    // 单手指缩放开始，不做任何处理
    if (e.touches.length == 1) return;
    let distance = this.calcDistance(e.touches[0], e.touches[1]);
    this.setData({
      'distance': distance,
    })
  },
  /**
   * 双手指移动   计算两个手指坐标和距离
   */
  touchmoveCallback: function (e) {
    // 单手指缩放不做任何操作
    if (e.touches.length == 1) return;
    let distance = this.calcDistance(e.touches[0], e.touches[1]);
    // 计算移动的过程中实际移动了多少的距离
    let distanceDiff = distance - this.data.distance;
    let newScale = this.data.scale + 0.005 * distanceDiff;
    
    if (newScale >= this.multiple && this.multiple > 2) { // 原图比较大情况
      newScale = this.multiple;
    } else if (this.multiple < 2 && newScale >= 2) { // 原图较小情况
      newScale = 2; // 最大2倍
    };
    // 最小缩放到0.3
    if (newScale <= 0.3) {
      newScale = 0.3;
    };
 
    let scaleWidth = newScale * this.data.initWidth;
    let scaleHeight = newScale * this.data.initHeight;
    this.setData({
      distance: distance,
      scale: newScale,
      scaleWidth: scaleWidth,
      scaleHeight: scaleHeight,
      diff: distanceDiff
    });
  },

/**
   * 计算两个手指距离
   */
  calcDistance(pos0, pos1) {
    let xMove = pos1.clientX - pos0.clientX;
    let yMove = pos1.clientY - pos0.clientY;
    return (Math.sqrt(xMove * xMove + yMove * yMove));
  },

    onLoad: function (options) {
        // 获取屏幕宽度
    this.width = wx.getSystemInfoSync().windowWidth;

        var that = this;
        console.log(options)
        that.setData({
            area: options.area,
            geneal: options.agent
        })
        if (options.page) {
            that.setData({
                page: options.page
            })
        }
        
    },

    getPhoneNumber(e) {
        var that = this;
            wx.cloud.callFunction({
                name: 'getMobile',
                data: {
                    weRunData: wx.cloud.CloudID(e.detail.cloudID),
                }
            }).then(res => {
                that.setData({
                    mobile: res.result,
                })
                wx.setStorageSync('phone', res.result)
                console.log(res.result)
                wx.request({ //发送请求
                    url: "https://zg99.offcn.com/index/biaodan/register/?actid=42173&callback=?",
                    data: {
                        name: that.data.inputValue,
                        xueli: that.data.xueLi,
                        yuanxiao:that.data.inputValueT,
                        phone: res.result,
                        fenxiao: that.data.area,
                        geneal: that.data.geneal
                    }, //接收参数
                    success(res) { //返回
                        var jsonStr = res.data;
                        jsonStr = jsonStr.replace("(", "");
                        jsonStr = jsonStr.replace(")", "");
                        if (typeof jsonStr != 'object') {
                            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                            var theJson = JSON.parse(jsonStr);
                        };
                        console.log(theJson)
         
    
                    }
                })
    
                wx.request({
                    url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/wx_collect_info',
                    method: 'POST',
                    data: {
                        activity_id: 30,
                        mobile: res.result,
                        area_code: that.data.area,
                        agent: that.data.geneal,
                        extra: {
                            dishi: that.data.dishi,
                            zkz: that.data.zkz,
                        },
                        page_index: that.data.page
                    },
                    success: res => {
                        console.log(res);
                    }
                })
    
            }).catch(err => {
                console.error(err);
            });
    },
    info(e) {
        wx.request({ //发送请求
            url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=35888&callback=?",
            data: {
                province: '山东省',
                city:'',
                limits:'500'
              
            }, //接收参数
            success(res) { //返回
                var jsonStr = res.data;
                jsonStr = jsonStr.replace("(", "");
                jsonStr = jsonStr.replace(")", "");
                if (typeof jsonStr != 'object') {
                    jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                    var theJson = JSON.parse(jsonStr);
                };
                console.log(theJson)

            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})